#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e3 + 10, M = 1e4 + 10;
int h[N], e[M * 2], ne[M * 2], idx;
int arr[N], backup[N];
int n, m;
int d[N];
void add(int a, int b)
{
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx++;
}
bool tuopu()
{
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	for (int i = 1; i <= n; i++) {
		int id = arr[i];
		if (backup[id] == 0) {
			for (int j = h[id]; ~j; j = ne[j]) {
				int z = e[j];
				backup[z]--;
			}
		}
		else {
			return 0;
		}
	}
	return 1;
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b;
		cin >> a >> b;
		d[b]++;
		add(a, b);
	}
	int k = 0;
	cin >> k;
	for (int i = 1; i <= k; i++) {
		memcpy(backup, d, sizeof(d));
		if (!tuopu()) {
			cout << i - 1 << " ";
		}
	}
	return 0;
}